﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using BusinessLayer;
using ModelLayer;
using ShopKart.Areas.Admin.Models;
using ShopKart.Common;
using ShopKart.Controllers;

namespace ShopKart.Areas.Admin.Controllers
{

    [AuthUser(Roles = "Admin")]
    public class ProductController : BaseController
    {
        // GET: Admin/Product
        public ActionResult Index()
        {
            ProductManager productManager = new ProductManager();
            ProductVm productVm = new ProductVm()
            {
                Products = productManager.GetAllProducts()
            };
            return View(productVm);
        }

        [HttpGet]
        public PartialViewResult GetAllProducts(long? productId)
        {
            ProductManager productManager = new ProductManager();
            ProductVm productVm = new ProductVm()
            {
                Products = productManager.GetAllProducts()
            };
            return PartialView("_GridProducts", productVm);
        }

        
        private static void FillProductVm(ProductVm productVm)
        {
            ProductManager productManager = new ProductManager();
            CategoryManager categoryManager = new CategoryManager();
            productVm.Categories = categoryManager.GetLeafCategories();
            productVm.Products = productManager.GetAllProducts();
        }

        [HttpGet]
        public PartialViewResult AddUpdateProduct(long? productId)
        {
            ProductManager productManager = new ProductManager();
            ProductVm productVm = new ProductVm();
            if (productId > 0)
            {
                var product = productManager.GetById(productId.Value);
                productVm = Mapper.Map<ProductVm>(product);
            }
            FillProductVm(productVm);

            return PartialView("_AddUpdateProduct", productVm);
        }

        [HttpPost]
        public ActionResult AddUpdateProduct(ProductVm productVm)
        {
            ProductManager productManager = new ProductManager();
            FillProductVm(productVm);

            var allErrors = ModelState.Values.SelectMany(v => v.Errors);
            if (!ModelState.IsValid)
            {
                return PartialView("_AddUpdateProduct", productVm);
            }


            string fileName = "";
            if (productVm.ProductImage != null)
            {
                fileName = Guid.NewGuid() + Path.GetExtension(productVm.ProductImage.FileName);
                var path = Path.Combine(GlobalConstants.ServerPath, fileName);
                productVm.ProductImage.SaveAs(path);
                productVm.Image = fileName;
            }

            var msg = productVm.ProductId > 0 ? "Product Updated Successfully" : "Product Added Successfully";

            var product = Mapper.Map<Product>(productVm);
            var result = productManager.AddUpdate(product);

            if (result) //Success
            {
                ShowMessage(msg, MessageTypeEnum.Success);
            }
            else //Error
            {
                ShowMessage("Some error occured", MessageTypeEnum.Error);

            }

            return PartialView("_AddUpdateProduct", productVm);
        }

        public JsonResult Delete(int productId)
        {
            ProductManager productManager = new ProductManager();
            bool result = productManager.Delete(productId);

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}